package com.lwei.tallybook.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwei.tallybook.base.domain.rel.RelBillBook;
import com.lwei.tallybook.service.mapper.rel.RelBillBookMapper;
import com.lwei.tallybook.service.service.RelBillBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RelBillBookServiceImpl extends ServiceImpl<RelBillBookMapper, RelBillBook> implements RelBillBookService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveBatch(List<RelBillBook> billBookList) {
        super.saveBatch(billBookList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(String billUuid, List<String> bookUuids) {
        List<RelBillBook> relBillBooks = bookUuids.stream().map(bookUuid ->
                new RelBillBook(bookUuid, billUuid)).collect(Collectors.toList());
        super.saveBatch(relBillBooks);
    }

    @Override
    public List<String> getBookByBill(String billUuid) {
        QueryWrapper<RelBillBook> wrapper = new QueryWrapper<>();
        wrapper.select("book_uuid");
        wrapper.eq("bill_uuid", billUuid);
        return super.list(wrapper).stream().map(RelBillBook::getBookUuid).collect(Collectors.toList());
    }

    @Override
    public List<String> getBillByBook(String bookUuid) {
        QueryWrapper<RelBillBook> wrapper = new QueryWrapper<>();
        wrapper.eq("book_uuid", bookUuid);
        return super.list(wrapper).stream().map(RelBillBook::getBillUuid).collect(Collectors.toList());
    }
}
